/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.fujiyuu75.sequent.sample;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.PopupDialog;
import ohos.app.Context;

/**
 * 菜单
 */
public class Menu extends PopupDialog {

    private Context context; // 上下文
    private String[] data; // 数据
    private float radiu = 10; // 圆角半径
    private int bgColor = 0xFFFFFFFF; // 背景颜色
    private int borderWidth = 1; // 边框宽度,单位px
    private int dividerHeight = 1; // 条目分隔线高度,单位px
    private int dividerColor = 0x000000FF; // 条目间隔线颜色
    private Boolean autoDismiss = true; // 操作完毕后是否自动关闭
    private int textSize = 16; // 条目文本大小，单位fp
    private int textColor = 0xFF000000; // 条目文本颜色
    private int left; // 条目文本颜色，默认8vp
    private int top; // 条目文本颜色，默认6vp
    private int right; // 条目文本颜色，默认8vp
    private int bottom; // 条目文本颜色，默认6vp
    private boolean textIsCenter = false; // 条目文本是否居中
    private ItemClickListener listener; // 条目点击事件

    private Menu(Context context, Component atConponent, String... data) {
        super(context, atConponent);
        this.context = context;
        this.data = data;
        left = vpToPx(8);
        top = vpToPx(6);
        right = vpToPx(8);
        bottom = vpToPx(6);
    }

    /**
     * 获取菜单对象
     *
     * @param context     上下文
     * @param atConponent 需要依附的控件
     * @param data        数据源
     * @return 菜单对象
     */
    public static Menu getInstance(Context context, Component atConponent, String... data) {
        return new Menu(context, atConponent, data);
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        setCustomComponent(getDialogContent());
        setCornerRadius(radiu);
    }

    private Component getDialogContent() {
        DirectionalLayout dialogConent = new DirectionalLayout(context);
        dialogConent.setLayoutConfig(new DirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT));
        dialogConent.setPadding(borderWidth, borderWidth, borderWidth, borderWidth);

        ListContainer listContainer = new ListContainer(context);
        listContainer.setLayoutConfig(new DirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT));
        listContainer.setBackground(getShapeElement(bgColor, radiu));
        dialogConent.addComponent(listContainer);

        listContainer.setItemProvider(new ItemProvider());

        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int position, long id) {
                if (listener != null) {
                    listener.onItemClick(component, position, data[position]);
                }
                if (autoDismiss) {
                    hide();
                }
            }
        });

        return dialogConent;
    }

    private class ItemProvider extends BaseItemProvider {
        @Override
        public int getCount() {
            return data != null ? data.length : 0;
        }

        @Override
        public String getItem(int position) {
            return data != null ? data[position] : "";
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public Component getComponent(int position, Component item, ComponentContainer parent) {
            item = new DirectionalLayout(context);
            item.setLayoutConfig(new DirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT));

            DependentLayout itemDe = new DependentLayout(context);
            itemDe.setLayoutConfig(new DependentLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT));
            itemDe.setPadding(left, top, right, bottom);

            Text text = new Text(context);
            text.setText(getItem(position));
            text.setTextSize(textSize, Text.TextSizeType.FP);
            text.setTextColor(new Color(textColor));
            if (textIsCenter) {
                DependentLayout.LayoutConfig layoutConfig1 = new DependentLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
                layoutConfig1.addRule(DependentLayout.LayoutConfig.CENTER_IN_PARENT);
                text.setLayoutConfig(layoutConfig1);
            }
            itemDe.addComponent(text);
            ((DirectionalLayout) item).addComponent(itemDe);

            if (position != getCount() - 1 && dividerHeight > 0) {
                DirectionalLayout line = new DirectionalLayout(context);
                line.setLayoutConfig(new DirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, dividerHeight));
                line.setBackground(getShapeElement(dividerColor, 0));
                ((DirectionalLayout) item).addComponent(line);
            }

            return item;
        }
    }

    /**
     * Menu设置为暗黑主题。
     *
     * @return 菜单对象自身
     */
    public Menu isDarkThem() {
        this.bgColor = 0x000000FF;
        this.dividerColor = 0xFFFFFFFF;
        this.textColor = 0xFFFFFFFF;
        return this;
    }

    /**
     * 设置菜单属性
     *
     * @param leftVp   上边距，单位vp
     * @param topVp    下边距，单位vp
     * @param rightVp  左边距，单位vp
     * @param bottomVp 右边距，单位vp
     * @return 菜单对象自身
     */
    public Menu setMenuPadding(int leftVp, int topVp, int rightVp, int bottomVp) {
        this.left = vpToPx(leftVp);
        this.top = vpToPx(topVp);
        this.right = vpToPx(rightVp);
        this.bottom = vpToPx(bottomVp);
        return this;
    }

    /**
     * 设置菜单边距
     *
     * @param radiu            圆角度数
     * @param bgColorRgba      背景颜色
     * @param borderWidthPx    边框宽度
     * @param dividerHeightPx  条目分隔线高度
     * @param dividerColorRgba 条目分隔线颜色
     * @param autoDismiss      点击条目后是否消失
     * @return 菜单对象自身
     */
    public Menu setMenuInfo(float radiu, int bgColorRgba, int borderWidthPx, int dividerHeightPx, int dividerColorRgba, Boolean autoDismiss) {
        this.radiu = radiu;
        this.bgColor = bgColorRgba;
        this.borderWidth = borderWidthPx;
        this.dividerHeight = dividerHeightPx;
        this.dividerColor = dividerColorRgba;
        this.autoDismiss = autoDismiss;
        return this;
    }

    /**
     * 设置条目样式
     *
     * @param textSizeFp    条目文字大小，单位fp
     * @param textColorArgb 条目文字颜色
     * @param textIsCenter  条目文字是否居中
     * @return 菜单对象自身
     */
    public Menu setItemTextStyle(int textSizeFp, int textColorArgb, boolean textIsCenter) {
        this.textSize = textSizeFp;
        this.textColor = textColorArgb;
        this.textIsCenter = textIsCenter;
        return this;
    }

    /**
     * 设置条目点击事件
     *
     * @param listener 监听
     * @return 菜单对象自身
     */
    public Menu setItemClickListener(ItemClickListener listener) {
        this.listener = listener;
        return this;
    }

    /**
     * 条目点击事件监听器
     */
    public interface ItemClickListener {
        void onItemClick(Component item, int position, String text);
    }

    private ShapeElement getShapeElement(int bgColor, float radiu) {
        ShapeElement drawable = new ShapeElement();
        drawable.setShape(ShapeElement.RECTANGLE);
        drawable.setRgbColor(new RgbColor(bgColor));
        drawable.setCornerRadius(radiu);
        return drawable;
    }

    private int vpToPx(float vp) {
        return (int) (context.getResourceManager().getDeviceCapability().screenDensity / 160 * vp);
    }

}
